package io.flash.rpc.client;

import io.flash.rpc.protocol.RpcRequestPacket;
import io.flash.rpc.protocol.RpcResponsePacket;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class Exchanges {

    private static Map<String, RpcRequestPacket> requestMap = new ConcurrentHashMap<>();

    private static Map<String, RpcResponsePacket> responseMap = new ConcurrentHashMap<>();


    public static void save(RpcRequestPacket rpcRequestPacket) {

        requestMap.put(rpcRequestPacket.getRequestId(), rpcRequestPacket);
    }

    public static RpcResponsePacket getResponse(RpcRequestPacket rpcRequestPacket) {

        return responseMap.get(rpcRequestPacket.getRequestId());
    }

    public static void addResponse(RpcResponsePacket rpcResponsePacket) {
        String requestId = rpcResponsePacket.getRequestId();
        responseMap.put(requestId, rpcResponsePacket);

        RpcRequestPacket rpcRequestPacket = requestMap.remove(requestId);

        rpcRequestPacket.receiveResponse();
    }
}
